package com.push.spider.spider;

import com.push.common.utils.StringUtils;
import com.push.common.utils.httpclient.HttpClientResult;
import com.push.common.utils.httpclient.HttpClientUtil;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SinaNewsSpider {
    public static void main(String[] args){
        HttpClientResult detailResult = HttpClientUtil.doGet("https://www.chinanews.com.cn/gj/2018/08-03/8588319.shtml", true);
    System.out.println(detailResult.getContent());
        Html detailhtml = null;
        try {
            detailhtml = new Html(convertEncoding_Str(detailResult.getContent(), "GB2312", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String summaryOrg = extractInfo("//td[2]/ul[@class='news_item']/li[@class='news_content']", detailhtml);
        String contentsOrg = extractInfo("//div[@class='left_zw']", detailhtml);
        String publisher = extractInfo("", detailhtml);
        String publishTimeNew = extractInfo("//div[@id='BaiduSpider']/span[@id='pubtime_baidu']/text()", detailhtml);
        System.out.println(summaryOrg);
        System.out.println(contentsOrg);
        System.out.println(publisher);
        System.out.println(publishTimeNew);
    }

    public static int getPages(Html html){
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        List<Selectable> nodes = html.xpath("//div[@id='pagediv']/a").nodes();
        for (Selectable selectable: nodes) {
            if (selectable.xpath("//a/text()").toString().equalsIgnoreCase("尾页")){
                Matcher m = p.matcher(selectable.xpath("//a/@href").toString());
                return Integer.valueOf(m.replaceAll("").trim());
            }
        }
        return 0;
    }

    public static String extractInfo(String xpathString, Selectable contentNode){
        if (StringUtils.isEmpty(xpathString)){
            return "";
        }
        List<String> xpathList = Arrays.asList(xpathString.split(";"));
        for (String xpath: xpathList){
            List<Selectable> nodes = contentNode.xpath(xpath).nodes();
            if(nodes.size() > 0){
                return nodes.get(0).toString();
            }
        }
        return "";
    }

    /** * 将原正确编码的字符串src，转化为编码为srcCharset的字符串 * * 前提是：确保原字符串的编码是无损（完整的）. 无需知道原字符串的具体编码， * 转化为目标编码的字符串由java库自动实现，无需自己手动实现。 * * 如果原编码字符串不能转化为目标编码，将会抛出UnsupportedEncodingException * * @param src * @param srcCharset * @param destCharet * @return 转换后的字符串 * @throws UnsupportedEncodingException */
    public static String convertEncoding_Str(String src,String srcCharset,String destCharet)
            throws UnsupportedEncodingException{
        byte[] bts = src.getBytes(destCharet);
        return new String(bts, destCharet);
    }


    /** * 将编码为srcCharset的字节数组src转化为编码为destCharet的字节数组 * * @param src * @param srcCharset * @param destCharet * @return * @throws UnsupportedEncodingException */
    public static byte[]  convertEncoding_ByteArr(byte[] src,String srcCharset,String destCharet)
            throws UnsupportedEncodingException{
        String s = new String(src, srcCharset);
        return s.getBytes(destCharet);
    }

    /** * 将字节数组byteArr转化为2位16进制字符串,每个16进制之间用空格分割 * * @param byteArr * @return */
    public static String byteToHex(byte... byteArr) {
        if (null == byteArr || byteArr.length == 0)
            return "";
        StringBuffer sb = new StringBuffer();
        String tmp = null;

        for (byte b : byteArr) {

            tmp = Integer.toHexString(b);
            // 切记：byte进行运算时，会自动转化为int，否则可能会出错
            if (b >>> 31 == 1) { // 最高位为1，负数
                sb.append(tmp.substring(6));
            } else { // 最高位为0，正数
                if(tmp.length() < 2)
                    sb.append('0');
                sb.append(tmp);
            }
            sb.append(' ');
        }
        sb.deleteCharAt(sb.length() - 1); // delete last space
        return sb.toString();
    }





}


